package dto

import (
	"jtdevice/api/device/model"
	"jtdevice/pkg/orm"
)

// 搜索条件
type Where struct {
	orm.DbPage
	StartTime string `form:"startTime"`
	EndTime   string `form:"endTime"`
	DeviceNo  string `form:"deviceNo"` //

	Guid      string `form:"guid"`     //
	LinkType  int    `form:"linkType"` //
	AlarmType int    `form:"alarmType"`

	Flag int    `form:"flag"` // 0-实时 1-补传
	Desc string `form:"desc"` //
}

// Where 初始化
func (o *Where) DbModelWhere(v interface{}, handlers ...func(dw *orm.DbWhere)) *orm.DbWhere {
	w := o.DbWhere()
	w.In("DeviceNo", o.DeviceNo)
	switch v.(type) {
	case *model.Online:
		w.TimeRange("OnlineTime", o.StartTime, o.EndTime)
		w.Orders = append(w.Orders, "OnlineTime desc")
	case *model.Alarm, *model.Status:
		w.TimeRange("DTU", o.StartTime, o.EndTime)
		w.Orders = append(w.Orders, "DTU desc")
	}
	for _, h := range handlers {
		h(w)
	}
	return w.Model(v)
}

// // Where 初始化
// func (s *AlarmPage) Where() string {
// 	tbname, _ := mnger.Devs.Model(s.DeviceNo)
// 	sql := "SELECT a.*, o.gps FROM t_devalarm a JOIN %s s" +
// 		" ON a.device_no like '%s' AND a.start_time >= '%s' AND a.start_time <= '%s' AND a.status_id = o.id ORDER BY a.start_time desc"
// 	sqlstr := fmt.Sprintf(sql, tbname, o.DeviceNo, o.StartTime, o.EndTime)
// 	return sqlstr
// }
